<?php
/**
 * User Behavior used to retrieve database information and user type.
 * 
 * @todo Use AuthItem system to emprove user rights.
 *
 * @version $Id: UserBehavior.php,v 1.3 2011-06-17 04:43:20 herve Exp $
 * @copyright 2011
 */
class UserBehavior extends CBehavior {

	public function canAccessRoleGroup($group)
	{
		$user = Yii::app()->user;

		if (!$user->isGuest) {
			$sql = <<<SQL
select count(*) nb
from (SELECT userid, itemname 
FROM `authassignment`
union all
select userid, child 
FROM authitemchild a, authassignment b
where a.parent = b.itemname
union all
select userid, b.child 
FROM authitemchild a, authitemchild b, authassignment c
where a.parent = c.itemname
and a.child = b.parent
) a 
where userid = :userid
and itemname like :itemname
SQL;
			$command=Yii::app()->db->createCommand($sql);
			$command->bindValue(':userid', $user->id);
			$command->bindValue(':itemname', $group . ".%");
			
			// check directly assigned items
			$row = $command->queryRow();
			if (isset($row) && is_array($row) && count($row) > 0)
			{
				return ($row['nb'] > 0);
			}
		}
		return false;
	}
	
	
/*	public function isAdministrator() {
		$user = Yii::app()->user;

		if (!$user->isGuest) {
			if ($user->hasState('dbuser')) {
				if ($user->getState('dbuser')->group->mnemonic == Constants::ADMIN_GROUP)
					return true;
			}
		}

		return false;
	}

	public function isAdvisor() {
		$user = Yii::app()->user;

		if (!$user->isGuest) {
			if ($user->hasState('dbuser')) {
				if ($user->getState('dbuser')->adviser != null)
					return true;
			}
		}

		return false;
	}

	public function isRoot() {
		$user = Yii::app()->user;

		if (!$user->isGuest) {
			if ($user->hasState('dbuser')) {
				if ($user->getState('dbuser')->group->mnemonic == Constants::ROOT_GROUP)
					return true;
			}
		}

		return false;
	} */
	
	public function getLanguage(){
		$user = Yii::app()->user;

		if (!$user->isGuest) {
			if ($user->hasState('dbuser')) {
				return ($user->getState('dbuser')->language);
			}
		}
		
		return 'fr'; //Yii::app()->sourceLanguage;
	}

}
?>
